<?php

/**
 * This is the model class for table "{{menu_items}}".
 *
 * The followings are the available columns in table '{{menu_items}}':
 * @property integer $id
 * @property string $name
 * @property integer $parent_id
 * @property integer $menu_id
 * @property integer $url
 * @property integer $disabled
 * @property integer $sort
 */
class MenuItems extends BaseModel
{
	public static function getLastId($model){
		$data = MenuItems::model()->findByAttributes(array('menu_id'=>$model->menu_id),array('order'=>'sort DESC'));
		return $data->sort+1;
	}
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return MenuItems the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{menu_items}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('name, menu_id, url', 'required'),
			array('parent_id, menu_id, disabled, sort', 'numerical', 'integerOnly'=>true),
			array('name, url', 'length', 'max'=>255),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, name, parent_id, menu_id, url, disabled, sort', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'menu' => array(self::BELONGS_TO, 'Menu', 'menu_id'),
			'lns' => array(self::HAS_MANY, 'MenuItemsLn', 'menu_item_id'),
			'ln' => array(self::HAS_ONE, 'MenuItemsLn', 'menu_item_id', 
				'condition'=>'ln.ln=:ln',
				'params'=>array(':ln'=>Yii::app()->language),
			),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id'		=>	Yii::t('main', 'ID'),
			'name' 		=>	Yii::t('main', 'Название'),
			'parent_id' =>	Yii::t('main', 'Родитель'),
			'menu_id' 	=>	Yii::t('main', 'Меню'),
			'url' 		=>	Yii::t('main', 'Ссылка'),
			'disabled' 	=>	Yii::t('main', 'Опубликовано'),
			'sort' 		=>	Yii::t('main', 'Сортировка'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('parent_id',$this->parent_id);
		$criteria->compare('menu_id',$this->menu_id);
		$criteria->compare('url',$this->url);
		$criteria->compare('disabled',$this->disabled);
		$criteria->compare('sort',$this->sort);
		$criteria->order = 'parent_id ASC, sort ASC';
		$result = new CActiveDataProvider($this, array(
			'pagination'=>array(
					'pageSize'=> Yii::app()->user->getState('pageSize', Yii::app()->params['defaultPageSize']),
			),
			'criteria'=>$criteria,
		));
		return $result;
	}
}